
;=================================================
; launches command interpreter flow
; operates with choices list which contains commands and
; its descriptions. Automatically adds `help' and `quit' options to the given
; options array
;
(defun launch-interpreter-flow (choices)

 ; defines helper function
 (defun show-help ()
  (println "usage:")
  (mapcar #'(lambda (cur-choice)
             (format t "~a:~12t~a~%" (first cur-choice) (second cur-choice)))
             choices)
  )

 ; add generic `exit' option
 (push (list (list "x" "exit" "q" "quit") "Leaves program" #'exit) choices)

 ; add generic `help' option
 (push (list (list "h" "help") "Shows this message" #'show-help) choices)
 
 ; interpreter loop
 (loop
  ; get user choice
  (setq cur-choice (prompt-read "Enter your choice or help to get help"))

  (setq action-not-found t)

  ; apply an associated action
  (mapcar #'(lambda (iter-choice)

             ; get command identifier
             (setq command-id (car iter-choice))

             ; if command identifier is an atom, make it a list for conformance
             (setq commands-list (if (atom command-id) (list command-id) command-id))

             ; walk through command list and do matching
             (dolist (cur-command commands-list)
              
              ; make case-insensitive matching
              (if (string-equal cur-choice cur-command)
               (or (setq action-not-found nil) (funcall (nth 2 iter-choice)))
              )
             ) ; dolist

            ) ; lambda
   choices) ; mapcar

  ; inform about wrong command
  (if action-not-found (println "Wrong command"))
 ) ; loop

 )

